package com.it.utils;

import com.it.constants.Constants;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.logging.Logger;

/**
 * @author : code1997
 * @date :2021-02-2021/2/4 21:16
 */
public class HBaseUtils {
    private static Logger logger = Logger.getLogger(HBaseUtils.class.getName());

    /**
     * create namespace
     */
    public static void createNameSpace(String nameSpace) throws IOException {
        logger.info("namespace===>" + nameSpace + ",创建成功！");
        Connection connection = ConnectionFactory.createConnection(Constants.CONFIGURATION);
        Admin admin = connection.getAdmin();
        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(nameSpace).build();
        admin.createNamespace(namespaceDescriptor);
        closeResource(admin, connection);
    }

    /**
     * check whether the table exists
     */
    public static boolean isTableExist(String tableName) throws IOException {
        Connection connection = ConnectionFactory.createConnection(Constants.CONFIGURATION);
        Admin admin = connection.getAdmin();
        boolean flag = admin.tableExists(TableName.valueOf(tableName));
        closeResource(admin, connection);
        return flag;
    }

    /**
     * close resource,such admin,connection
     */
    public static void closeResource(Admin admin, Connection connection, Table... tables) throws IOException {
        if (tables != null) {
            for (Table table : tables) {
                if (table != null) {
                    table.close();
                }
            }
        }
        if (admin != null) {
            admin.close();
        }
        if (connection != null) {
            connection.close();
        }
    }

    /**
     * create hbase table
     */
    public static void createTable(String tableName, int version, String... columnFamily) throws IOException {
        if (columnFamily == null || columnFamily.length <= 0) {
            System.out.println("请设置列族信息!");
            return;
        }
        if (isTableExist(tableName)) {
            System.out.println("该表已经存在!");
            return;
        }
        logger.info("table===>" + tableName + ",创建成功！");
        Connection connection = ConnectionFactory.createConnection(Constants.CONFIGURATION);
        Admin admin = connection.getAdmin();
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);

        for (String column : columnFamily) {
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(column);
            hColumnDescriptor.setMaxVersions(version);
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        admin.createTable(hTableDescriptor);
        closeResource(admin, connection);
    }

}
